# Author    : CoffeeChicken
# Date      : 2020-10-08 17:14
# Function  : 演示 xpath案例

import requests
from lxml import etree

url = "https://bj.58.com/ershoufang/"

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/85.0.4183.121 Safari/537.36"
}

page_text = requests.get(url, headers).text

tree = etree.HTML(page_text)
# 直接定位分析出页面标题所在处
# res_list = tree.xpath("//h2[@class='title']/a/text()")
# for title in res_list:
#     print(title)

# 两次定位找出
res_list = tree.xpath("//ul[@class='house-list-wrap']/li")
for li in res_list:
    title_text = li.xpath("./div[2]/h2/a/text()")[0]
    print(title_text)



